using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using PickGold.Charting.Utilities;

namespace PickGold.Charting.Design
{
	internal class KeywordEditor : Form
	{
		private ArrayList _applicableKeywords;
		private Button _buttonCancel;
		private Button _buttonOk;
		private ComboBox _comboBoxFormat;
		private IContainer _components;
		private GroupBox _groupBoxDescription;
		private GroupBox _groupBoxFormat;
		private GroupBox _groupBoxKeywords;
		private Label _labelCustomFormat;
		private Label _labelDescription;
		private Label _labelFormat;
		private Label _labelPrecision;
		private Label _labelSample;
		private Label _labelYValue;
		private ListBox _listBoxKeywords;
		private int _maxYValueIndex;
		private NumericUpDown _numericUpDownYValue;
		private TextBox _textBoxCustomFormat;
		private TextBox _textBoxPrecision;
		private TextBox _textBoxSample;
		private ToolTip _toolTip;
		internal string Keyword;

		public KeywordEditor()
		{
			this.Keyword = string.Empty;
			this._maxYValueIndex = 9;
			this.InitializeComponent();
		}

		public KeywordEditor(ArrayList applicableKeywords, string keyword, int maxYValueIndex)
		{
			this.Keyword = string.Empty;
			this._maxYValueIndex = 9;
			this.InitializeComponent();
			this._applicableKeywords = applicableKeywords;
			this.Keyword = keyword;
			this._maxYValueIndex = maxYValueIndex;
		}

		private void buttonOk_Click(object sender, EventArgs e)
		{
			this.Keyword = string.Empty;
			KeywordInfo selectedItem = this._listBoxKeywords.SelectedItem as KeywordInfo;
			if (selectedItem != null)
			{
				this.Keyword = selectedItem.Keyword;
				if (selectedItem.SupportsValueIndex && (((int)this._numericUpDownYValue.Value) > 0))
				{
					this.Keyword = this.Keyword + "Y" + ((int)this._numericUpDownYValue.Value).ToString(CultureInfo.InvariantCulture);
				}
				if ((selectedItem.SupportsFormatting && (this._comboBoxFormat.SelectedIndex > 0)) && (this.GetFormatString().Length > 0))
				{
					this.Keyword = this.Keyword + "{" + this.GetFormatString() + "}";
				}
			}
		}

		private void comboBoxFormat_SelectedIndexChanged(object sender, EventArgs e)
		{
			this._labelCustomFormat.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			this._textBoxCustomFormat.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			this._labelPrecision.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			this._textBoxPrecision.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			this._labelSample.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			this._textBoxSample.Enabled = this._comboBoxFormat.SelectedIndex > 0;
			bool flag = ((string)this._comboBoxFormat.SelectedItem) == "Custom";
			this._labelCustomFormat.Visible = flag;
			this._textBoxCustomFormat.Visible = flag;
			this._labelPrecision.Visible = !flag;
			this._textBoxPrecision.Visible = !flag;
			this.UpdateNumericSample();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && (this._components != null))
			{
				this._components.Dispose();
			}
			base.Dispose(disposing);
		}

		private string GetFormatString()
		{
			string text = string.Empty;
			if (this._comboBoxFormat.Enabled && (this._comboBoxFormat.SelectedIndex == 1))
			{
				return ("C" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 2)
			{
				return ("D" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 3)
			{
				return ("E" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 4)
			{
				return ("G" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 5)
			{
				return ("G" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 6)
			{
				return ("N" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 7)
			{
				return ("P" + this._textBoxPrecision.Text);
			}
			if (this._comboBoxFormat.SelectedIndex == 8)
			{
				text = this._textBoxCustomFormat.Text;
			}
			return text;
		}

		private void InitializeComponent()
		{
			this._components = new Container();
			this._groupBoxKeywords = new GroupBox();
			this._listBoxKeywords = new ListBox();
			this._groupBoxDescription = new GroupBox();
			this._labelDescription = new Label();
			this._buttonCancel = new Button();
			this._buttonOk = new Button();
			this._groupBoxFormat = new GroupBox();
			this._textBoxPrecision = new TextBox();
			this._labelSample = new Label();
			this._textBoxSample = new TextBox();
			this._numericUpDownYValue = new NumericUpDown();
			this._labelYValue = new Label();
			this._comboBoxFormat = new ComboBox();
			this._labelPrecision = new Label();
			this._labelFormat = new Label();
			this._labelCustomFormat = new Label();
			this._textBoxCustomFormat = new TextBox();
			this._toolTip = new ToolTip(this._components);
			this._groupBoxKeywords.SuspendLayout();
			this._groupBoxDescription.SuspendLayout();
			this._groupBoxFormat.SuspendLayout();
			this._numericUpDownYValue.BeginInit();
			base.SuspendLayout();
			this._groupBoxKeywords.Controls.AddRange(new Control[] { this._listBoxKeywords });
			this._groupBoxKeywords.Location = new Point(8, 0x10);
			this._groupBoxKeywords.Name = "groupBoxKeywords";
			this._groupBoxKeywords.Size = new Size(0xd8, 0xe8);
			this._groupBoxKeywords.TabIndex = 0;
			this._groupBoxKeywords.TabStop = false;
			this._groupBoxKeywords.Text = SR.LabelKeyKeywords;
			this._listBoxKeywords.Location = new Point(8, 0x18);
			this._listBoxKeywords.Name = "listBoxKeywords";
			this._listBoxKeywords.Size = new Size(200, 0xc7);
			this._listBoxKeywords.TabIndex = 0;
			this._listBoxKeywords.DoubleClick += new EventHandler(this.listBoxKeywords_DoubleClick);
			this._listBoxKeywords.SelectedIndexChanged += new EventHandler(this.listBoxKeywords_SelectedIndexChanged);
			this._groupBoxDescription.Controls.AddRange(new Control[] { this._labelDescription });
			this._groupBoxDescription.Location = new Point(240, 0x10);
			this._groupBoxDescription.Name = "groupBoxDescription";
			this._groupBoxDescription.Size = new Size(0x148, 0x58);
			this._groupBoxDescription.TabIndex = 1;
			this._groupBoxDescription.TabStop = false;
			this._groupBoxDescription.Text = SR.LabelDescription;
			this._labelDescription.Location = new Point(0x10, 0x18);
			this._labelDescription.Name = "labelDescription";
			this._labelDescription.Size = new Size(0x130, 0x38);
			this._labelDescription.TabIndex = 0;
			this._labelDescription.Text = "<replaced at runtime>";
			this._buttonCancel.DialogResult = DialogResult.Cancel;
			this._buttonCancel.Location = new Point(0x134, 260);
			this._buttonCancel.Name = "buttonCancel";
			this._buttonCancel.TabIndex = 4;
			this._buttonCancel.Text = SR.LabelButtonCancel;
			this._buttonOk.DialogResult = DialogResult.OK;
			this._buttonOk.Location = new Point(0xc4, 260);
			this._buttonOk.Name = "buttonOk";
			this._buttonOk.TabIndex = 3;
			this._buttonOk.Text = SR.LabelButtonOk;
			this._buttonOk.Click += new EventHandler(this.buttonOk_Click);
			this._groupBoxFormat.Controls.AddRange(new Control[] { this._textBoxPrecision, this._labelSample, this._textBoxSample, this._numericUpDownYValue, this._labelYValue, this._comboBoxFormat, this._labelPrecision, this._labelFormat, this._labelCustomFormat, this._textBoxCustomFormat });
			this._groupBoxFormat.Location = new Point(240, 0x70);
			this._groupBoxFormat.Name = "groupBoxFormat";
			this._groupBoxFormat.Size = new Size(0x148, 0x88);
			this._groupBoxFormat.TabIndex = 2;
			this._groupBoxFormat.TabStop = false;
			this._groupBoxFormat.Text = SR.LabelValueFormatting;
			this._textBoxPrecision.Location = new Point(0x70, 0x30);
			this._textBoxPrecision.Name = "textBoxPrecision";
			this._textBoxPrecision.Size = new Size(0x40, 20);
			this._textBoxPrecision.TabIndex = 3;
			this._textBoxPrecision.Text = "";
			this._textBoxPrecision.TextChanged += new EventHandler(this.textBoxPrecision_TextChanged);
			this._labelSample.Location = new Point(8, 0x48);
			this._labelSample.Name = "labelSample";
			this._labelSample.Size = new Size(0x60, 0x17);
			this._labelSample.TabIndex = 7;
			this._labelSample.Text = SR.LabelFormatKeySample;
			this._labelSample.TextAlign = ContentAlignment.MiddleRight;
			this._textBoxSample.Location = new Point(0x70, 0x48);
			this._textBoxSample.Name = "textBoxSample";
			this._textBoxSample.ReadOnly = true;
			this._textBoxSample.Size = new Size(0xc0, 20);
			this._textBoxSample.TabIndex = 8;
			this._textBoxSample.Text = "";
			this._numericUpDownYValue.CausesValidation = false;
			this._numericUpDownYValue.Location = new Point(0x70, 0x68);
			int[] bits = new int[4];
			bits[0] = 9;
			this._numericUpDownYValue.Maximum = new decimal(bits);
			this._numericUpDownYValue.Name = "numericUpDownYValue";
			this._numericUpDownYValue.Size = new Size(0x40, 20);
			this._numericUpDownYValue.TabIndex = 10;
			this._numericUpDownYValue.ValueChanged += new EventHandler(this.numericUpDownYValue_ValueChanged);
			this._labelYValue.Location = new Point(8, 0x68);
			this._labelYValue.Name = "labelYValue";
			this._labelYValue.Size = new Size(0x60, 0x17);
			this._labelYValue.TabIndex = 9;
			this._labelYValue.Text = SR.LabelKeyYValueIndex;
			this._labelYValue.TextAlign = ContentAlignment.MiddleRight;
			this._comboBoxFormat.DropDownStyle = ComboBoxStyle.DropDownList;
			this._comboBoxFormat.Items.AddRange(new object[] { SR.DescriptionTypeNone, SR.DescriptionNumberFormatTypeCurrency, SR.DescriptionNumberFormatTypeDecimal, SR.DescriptionNumberFormatTypeScientific, SR.DescriptionNumberFormatTypeFixedPoint, SR.DescriptionNumberFormatTypeGeneral, SR.DescriptionNumberFormatTypeNumber, SR.DescriptionNumberFormatTypePercent, SR.DescriptionTypeCustom });
			this._comboBoxFormat.Location = new Point(0x70, 0x18);
			this._comboBoxFormat.MaxDropDownItems = 10;
			this._comboBoxFormat.Name = "comboBoxFormat";
			this._comboBoxFormat.Size = new Size(0xc0, 0x15);
			this._comboBoxFormat.TabIndex = 1;
			this._comboBoxFormat.SelectedIndexChanged += new EventHandler(this.comboBoxFormat_SelectedIndexChanged);
			this._labelPrecision.Location = new Point(8, 0x30);
			this._labelPrecision.Name = "labelPrecision";
			this._labelPrecision.Size = new Size(0x60, 0x17);
			this._labelPrecision.TabIndex = 2;
			this._labelPrecision.Text = SR.LabelKeyPrecision;
			this._labelPrecision.TextAlign = ContentAlignment.MiddleRight;
			this._labelFormat.Location = new Point(8, 0x18);
			this._labelFormat.Name = "labelFormat";
			this._labelFormat.Size = new Size(0x60, 0x17);
			this._labelFormat.TabIndex = 0;
			this._labelFormat.Text = SR.LabelKeyFormat;
			this._labelFormat.TextAlign = ContentAlignment.MiddleRight;
			this._labelCustomFormat.Location = new Point(8, 0x30);
			this._labelCustomFormat.Name = "labelCustomFormat";
			this._labelCustomFormat.Size = new Size(0x60, 0x17);
			this._labelCustomFormat.TabIndex = 4;
			this._labelCustomFormat.Text = SR.LabelKeyCustomFormat;
			this._labelCustomFormat.TextAlign = ContentAlignment.MiddleRight;
			this._labelCustomFormat.Visible = false;
			this._textBoxCustomFormat.Location = new Point(0x70, 0x30);
			this._textBoxCustomFormat.Name = "textBoxCustomFormat";
			this._textBoxCustomFormat.Size = new Size(0xc0, 20);
			this._textBoxCustomFormat.TabIndex = 5;
			this._textBoxCustomFormat.Text = "";
			this._textBoxCustomFormat.Visible = false;
			this._textBoxCustomFormat.TextChanged += new EventHandler(this.textBoxCustomFormat_TextChanged);
			base.AcceptButton = this._buttonOk;
			this.AutoScaleBaseSize = new Size(5, 13);
			base.CancelButton = this._buttonCancel;
			base.ClientSize = new Size(0x242, 0x127);
			base.Controls.AddRange(new Control[] { this._groupBoxFormat, this._buttonCancel, this._buttonOk, this._groupBoxDescription, this._groupBoxKeywords });
			base.FormBorderStyle = FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "KeywordEditor";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = SR.LabelKeywordEditor;
			base.Load += new EventHandler(this.KeywordEditor_Load);
			this._groupBoxKeywords.ResumeLayout(false);
			this._groupBoxDescription.ResumeLayout(false);
			this._groupBoxFormat.ResumeLayout(false);
			this._numericUpDownYValue.EndInit();
			base.ResumeLayout(false);
		}

		private void KeywordEditor_Load(object sender, EventArgs e)
		{
			if ((this._maxYValueIndex >= 0) && (this._maxYValueIndex < 10))
			{
				this._numericUpDownYValue.Maximum = this._maxYValueIndex;
			}
			this._numericUpDownYValue.Enabled = this._maxYValueIndex > 0;
			this._labelYValue.Enabled = this._maxYValueIndex > 0;
			this._toolTip.SetToolTip(this._textBoxCustomFormat, SR.DescriptionToolTipCustomFormatCharacters);
			this._comboBoxFormat.SelectedIndex = 0;
			if (this._applicableKeywords != null)
			{
				foreach (KeywordInfo info in this._applicableKeywords)
				{
					this._listBoxKeywords.Items.Add(info);
				}
			}
			if (this.Keyword.Length == 0)
			{
				this._listBoxKeywords.SelectedIndex = 0;
				this._comboBoxFormat.SelectedIndex = 0;
			}
			else
			{
				bool flag = false;
				foreach (KeywordInfo info2 in this._applicableKeywords)
				{
					foreach (string str in info2.GetKeywords())
					{
						if (this.Keyword.StartsWith(str, StringComparison.Ordinal))
						{
							this._listBoxKeywords.SelectedItem = info2;
							int length = str.Length;
							if ((info2.SupportsValueIndex && (this.Keyword.Length > length)) && (this.Keyword[length] == 'Y'))
							{
								length++;
								if ((this.Keyword.Length > length) && char.IsDigit(this.Keyword[length]))
								{
									int num2 = int.Parse(this.Keyword.Substring(length, 1), CultureInfo.InvariantCulture);
									if ((num2 < 0) || (num2 > this._maxYValueIndex))
									{
										num2 = 0;
									}
									this._numericUpDownYValue.Value = num2;
									length++;
								}
							}
							if ((info2.SupportsFormatting && (this.Keyword.Length > length)) && ((this.Keyword[length] == '{') && this.Keyword.EndsWith("}", StringComparison.Ordinal)))
							{
								string str2 = this.Keyword.Substring(length + 1, (this.Keyword.Length - length) - 2);
								if (str2.Length == 0)
								{
									this._comboBoxFormat.SelectedIndex = 0;
								}
								else if (((str2.Length == 1) || ((str2.Length == 2) && char.IsDigit(str2[1]))) || ((str2.Length == 3) && char.IsDigit(str2[2])))
								{
									if (str2[0] == 'C')
									{
										this._comboBoxFormat.SelectedIndex = 1;
									}
									else if (str2[0] == 'D')
									{
										this._comboBoxFormat.SelectedIndex = 2;
									}
									else if (str2[0] == 'E')
									{
										this._comboBoxFormat.SelectedIndex = 3;
									}
									else if (str2[0] == 'F')
									{
										this._comboBoxFormat.SelectedIndex = 4;
									}
									else if (str2[0] == 'G')
									{
										this._comboBoxFormat.SelectedIndex = 5;
									}
									else if (str2[0] == 'N')
									{
										this._comboBoxFormat.SelectedIndex = 6;
									}
									else if (str2[0] == 'P')
									{
										this._comboBoxFormat.SelectedIndex = 7;
									}
									else
									{
										this._comboBoxFormat.SelectedIndex = 8;
										this._textBoxCustomFormat.Text = str2;
									}
									if ((this._comboBoxFormat.SelectedIndex != 8) && (str2.Length > 0))
									{
										this._textBoxPrecision.Text = str2.Substring(1);
									}
								}
								else
								{
									this._comboBoxFormat.SelectedIndex = 8;
									this._textBoxCustomFormat.Text = str2;
								}
							}
							flag = true;
							break;
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
		}

		private void listBoxKeywords_DoubleClick(object sender, EventArgs e)
		{
			base.AcceptButton.PerformClick();
		}

		private void listBoxKeywords_SelectedIndexChanged(object sender, EventArgs e)
		{
			KeywordInfo selectedItem = this._listBoxKeywords.SelectedItem as KeywordInfo;
			if (selectedItem != null)
			{
				this._labelDescription.Text = selectedItem.Description.Replace(@"\n", "\n");
				this._groupBoxFormat.Enabled = selectedItem.SupportsFormatting;
				this._labelYValue.Enabled = selectedItem.SupportsValueIndex;
				this._numericUpDownYValue.Enabled = selectedItem.SupportsValueIndex && (this._maxYValueIndex > 0);
				this._labelYValue.Enabled = selectedItem.SupportsValueIndex && (this._maxYValueIndex > 0);
			}
		}

		private void numericUpDownYValue_ValueChanged(object sender, EventArgs e)
		{
			if ((this._numericUpDownYValue.Value > this._maxYValueIndex) && (this._numericUpDownYValue.Value < 0M))
			{
				MessageBoxOptions options = 0;
				if (this.RightToLeft == RightToLeft.Yes)
				{
					options = MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
				}
				MessageBox.Show(this, SR.MessageYValueIndexInvalid(this._maxYValueIndex.ToString(CultureInfo.CurrentCulture)), SR.MessageChartTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, options);
				this._numericUpDownYValue.Value = 0M;
			}
		}

		private void textBoxCustomFormat_TextChanged(object sender, EventArgs e)
		{
			this.UpdateNumericSample();
		}

		private void textBoxPrecision_TextChanged(object sender, EventArgs e)
		{
			MessageBoxOptions options = 0;
			if (this.RightToLeft == RightToLeft.Yes)
			{
				options = MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
			}
			if ((this._textBoxPrecision.Text.Length >= 1) && !char.IsDigit(this._textBoxPrecision.Text[0]))
			{
				MessageBox.Show(this, SR.MessagePrecisionInvalid, SR.MessageChartTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, options);
				this._textBoxPrecision.Text = "";
			}
			else if ((this._textBoxPrecision.Text.Length >= 2) && (!char.IsDigit(this._textBoxPrecision.Text[0]) || !char.IsDigit(this._textBoxPrecision.Text[1])))
			{
				MessageBox.Show(this, SR.MessagePrecisionInvalid, SR.MessageChartTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, options);
				this._textBoxPrecision.Text = "";
			}
			this.UpdateNumericSample();
		}

		private void UpdateNumericSample()
		{
			string formatString = this.GetFormatString();
			if (this._comboBoxFormat.SelectedIndex == 0)
			{
				this._textBoxSample.Text = string.Empty;
			}
			else if (this._comboBoxFormat.SelectedIndex == 1)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 2)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 0x3039 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 3)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 4)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 5)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 6)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 7)
			{
				this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 0.126 });
			}
			else if (this._comboBoxFormat.SelectedIndex == 8)
			{
				bool flag = false;
				try
				{
					this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 12345.6789 });
					flag = true;
				}
				catch (FormatException)
				{
				}
				if (!flag)
				{
					try
					{
						this._textBoxSample.Text = string.Format(CultureInfo.CurrentCulture, "{0:" + formatString + "}", new object[] { 0x3039 });
						flag = true;
					}
					catch (FormatException)
					{
					}
				}
				if (!flag)
				{
					this._textBoxSample.Text = SR.DesciptionCustomLabelFormatInvalid;
				}
			}
		}
	}
}

